package com.game.framework.message.event;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GameMessageListenerDispatchManager {
    // 用于存放事件与事件监听器的关系
    private Map<String, List<IGameMessageListener>> eventListenerMap = new HashMap<>();

    public GameMessageListenerDispatchManager() {
    }

    // 用于注册事件监听器
    public void registerListener(Class<? extends IGameMessage> eventClass, IGameMessageListener listener) {
        String key = eventClass.getName();
        List<IGameMessageListener> eventListeners = eventListenerMap.computeIfAbsent(key, k -> new ArrayList<>());
        eventListeners.add(listener);
    }

    public void sendGameEvent(Object origin, IGameMessage gameEventMessage) {
        String key = gameEventMessage.getClass().getName();
        List<IGameMessageListener> eventListeners = eventListenerMap.get(key);
        if (eventListeners != null && eventListeners.size() > 0) {
            eventListeners.forEach(listener -> listener.update(origin, gameEventMessage));
        }
    }
}

